package pers.sports.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pers.sports.dao.ManagerDao;
import pers.sports.entity.Manager;
import pers.sports.service.ManagerService;
import pers.sports.util.MD5Util;
import pers.sports.vo.LoginInfo;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ManagerServiceImpl implements ManagerService {

    private final ManagerDao managerDao;

    @Autowired
    public ManagerServiceImpl(ManagerDao managerDao) {
        this.managerDao = managerDao;
    }


    @Override
    @Transactional(readOnly = true)
    public Boolean ManagerPwdByNum(LoginInfo loginInfo) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwd;
        if (managerDao.findManagerByNum(loginInfo.getLoginName()) == null) {
            return false;
        } else {
            pwd = managerDao.findManagerByNum(loginInfo.getLoginName()).getManagerPwd();
        }
        String md5pwd = MD5Util.encoderPassword(loginInfo.getPwd());
        return pwd.equals(md5pwd);
    }

    @Override
    public List<Manager> finall(Integer page, Integer limit) {
        Integer b = (page-1)+((page-1)*limit);
        return managerDao.findAll(b, limit);
    }

    @Override
    public String findIdentityByName(String loginName) {
        return managerDao.findIdentityByName(loginName);
    }

    @Override
    public Boolean findNumByManagerNum(String managerNum) {
        return managerDao.findNumByManagerNum(managerNum) == 0 ? true : false;
    }

    @Override
    public Boolean insertManager(Manager manager) {
        return managerDao.insertManager(manager) != 0 ? true : false;
    }

    @Override
    public void updatePwdByNum(String number, String new_password) {
        managerDao.updatePwdByNum(number, new_password);
    }

    @Override
    public Integer countNumber() {
        return managerDao.countNumber();
    }

    @Override
    public boolean deleteById(String id) {
        return managerDao.deleteById(id)!=0?true:false;
    }


}
